<template>
  <el-form :model="user" :rules="loginRules" ref="user" label-position="left"  class="login-container">
    <h3 class="title">系统登录</h3>
    <el-form-item prop="username">
      <el-input type="text"  v-model="user.username" auto-complete="off" placeholder="请输入账号"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password"  v-model="user.password" auto-complete="off" placeholder="请输入密码"
                @keyup.enter.native="submit"></el-input>
    </el-form-item>
    <el-form-item style="width:100%;">
      <el-button type="primary" style="width:100%;" @click.native.prevent="submit" :loading="logining">登录</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import * as api from "../../../api/api";
  import {mapActions} from 'vuex';
    export default {
        name: "login",
      data(){
        let validatePass = (rule, value, callback) => {
          if (!value) {
            callback(new Error('请输入密码'));
          } else {
            callback();
          }
        };
        let validateAccount = (rule, value, callback) => {
          if (!value) {
            callback(new Error('请输入账号'));
          }  else {
            callback();
          }
        };
          return {logining: false,user:{
              username:'',password:''
            },loginRules:{
              username:[{required:true , validator: validateAccount, trigger: 'blur'}],
              password:[{ required:true,validator: validatePass, trigger: 'blur' }]
            }};
      },
      methods:{
        ...mapActions([
          'login'
        ]),
          submit:function () {
            this.$refs['user'].validate((valid) => {
              if (!valid) {
                console.log('error submit!!');
                return false;
              } else {
                let redirect = decodeURIComponent(this.$route.query.redirect || '/');
                this.login(this.user).then(()=>{
                  location.href = "http://120.92.15.43:8000"+redirect;
                  //this.$router.push({path: redirect});
                  }
                );

                // api.requestLogin({account:this.user.fname,pwd:this.user.pwd}).then((response) =>{
                //   if(response.data.state===0){
                //     this.$message({
                //       message: response.data.msg,
                //       center: true,
                //       type:'error'
                //     });
                //   }else{
                //     let redirect = decodeURIComponent(this.$route.query.redirect || '/');
                //     this.$store.dispatch('login',response.data);
                //     this.$router.push({ path: redirect });
                //   }
                // }).catch((error)=> {
                //   console.log(error);
                // });
              }
            });
          }
      }
    }
</script>

<style>
  .login-container {
    /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }
  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .remember {
    margin: 0px 0px 35px 0px;
  }
</style>
